﻿using RPGData.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using SqlHelper;

namespace RPGData
{
    public class PlayerService
    {
        /// <summary>
        /// 根据机器码获取玩家信息
        /// </summary>
        /// <param name="machineId">机器码</param>
        /// <returns>Player</returns>
        public Player GetPlayerByMachineId(string machineId)
        {
            string sql = "select * from Player where MachineId = @MachineId";
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@MachineId",machineId)
            };

            DataTable dt = SQLHELP.GetDataTableWithParm(sql, parm);

            Player player = new Player();
            player.Id = int.Parse(dt.Rows[0]["Id"].ToString());
            player.MachineId = dt.Rows[0]["MachineId"].ToString();
            player.Account = dt.Rows[0]["Account"].ToString();
            player.Password = dt.Rows[0]["Password"].ToString();
            player.CreatedDate = DateTime.Parse(dt.Rows[0]["CreatedDate"].ToString());
            player.Level = int.Parse(dt.Rows[0]["Level"].ToString());
            player.NickName = dt.Rows[0]["NickName"].ToString();
            player.Status = int.Parse(dt.Rows[0]["Status"].ToString());

            return player;
        }

        /// <summary>
        /// 为玩家绑定一个账号
        /// </summary>
        /// <param name="playerId">playerId</param>
        /// <param name="account">账号</param>
        /// <param name="password">密码</param>
        /// <returns>bool</returns>
        public bool BindAccount(int playerId, string account, string password)
        {
            string sql = "update Player set Account = @Account, Password=@Password where Id=@Id";
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@Account",account),
                new SqlParameter("@Password",password),
                new SqlParameter("@Id",playerId)
            };
            int count = SQLHELP.GetNonQueryWithParm(sql, parm);

            if (count != 1)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 通过账号和密码来获取玩家信息
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="password">密码</param>
        /// <returns>Player</returns>
        public Player GetPlayerByAccount(string account,string password)
        {
            string sql = "select * from Player where Account = @Account and Password=@Password";
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@Account",account),
                new SqlParameter("@Password",password)
            };

            DataTable dt = SQLHELP.GetDataTableWithParm(sql, parm);

            Player player = new Player();
            player.Id = int.Parse(dt.Rows[0]["Id"].ToString());
            player.MachineId = dt.Rows[0]["MachineId"].ToString(); ;
            player.Account = dt.Rows[0]["Account"].ToString();
            player.Password = dt.Rows[0]["Password"].ToString();
            player.CreatedDate = DateTime.Parse(dt.Rows[0]["CreatedDate"].ToString());
            player.Level = int.Parse(dt.Rows[0]["Level"].ToString());
            player.NickName = dt.Rows[0]["NickName"].ToString();
            player.Status = int.Parse(dt.Rows[0]["Status"].ToString());

            return player;
        }

        /// <summary>
        /// 设置玩家的昵称
        /// </summary>
        /// <param name="playerId">playerId</param>
        /// <param name="nickName">昵称</param>
        /// <returns>bool</returns>
        public bool SetPlayerNickName(int playerId, string nickName)
        {
            string sql = "update Player set nickName = @nickName where Id=@Id";
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@NickName",nickName),
                new SqlParameter("@Id",playerId)
            };
            int count = SQLHELP.GetNonQueryWithParm(sql, parm);

            if (count != 1)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 获取玩家的单位列表
        /// </summary>
        /// <param name="playerId">playerId</param>
        /// <returns>PlayerObject集合</returns>
        public List<PlayerObject> GetPlayerObjects(int playerId)
        {
            string sql = "select * from PlayerObject where PlayerId =@PlayerId";
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@PlayerId",playerId)
            };

            DataTable dt = SQLHELP.GetDataTableWithParm(sql, parm);

            List<PlayerObject> list = new List<PlayerObject>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                PlayerObject obj = new PlayerObject();
                obj.Id = int.Parse(dt.Rows[i]["Id"].ToString());
                obj.PlayerId = int.Parse(dt.Rows[i]["PlayerId"].ToString());
                obj.Name = dt.Rows[i]["Name"].ToString();
                obj.Level = int.Parse(dt.Rows[i]["Level"].ToString());
                obj.CreatedDate = DateTime.Parse(dt.Rows[i]["CreatedDate"].ToString());
                obj.MapId = int.Parse(dt.Rows[i]["MapId"].ToString());
                obj.PosRow = int.Parse(dt.Rows[i]["PosRow"].ToString());
                obj.PosCol = int.Parse(dt.Rows[i]["PosCol"].ToString());
                obj.DirectionId = int.Parse(dt.Rows[i]["DirectionId"].ToString());
                obj.Experience = double.Parse(dt.Rows[i]["Experience"].ToString());

                list.Add(obj);
            }

            return list;
        }

        /// <summary>
        /// 为玩家创建一个单位,并返回主键
        /// </summary>
        /// <param name="obj">PlayerObject对象</param>
        /// <returns>主键</returns>
        public int CreatePlayerObject(PlayerObject obj)
        {
            string sql = "insert PlayerObject(PlayerId,Name,Level,CreatedDate,MapId,PosRow,PosCol,DirectionId,Experience) values(@PlayerId,@Name,@Level,@CreatedDate,@MapId,@PosRow,@PosCol,@DirectionId,@Experience);Select @@IDENTITY as returnId;";

            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@PlayerId",obj.PlayerId),
                new SqlParameter("@Name",obj.Name),
                new SqlParameter("@Level",obj.Level),
                new SqlParameter("@CreatedDate",obj.CreatedDate),
                new SqlParameter("@MapId",obj.MapId),
                new SqlParameter("@PosRow",obj.PosRow),
                new SqlParameter("@PosCol",obj.PosCol),
                new SqlParameter("@DirectionId",obj.DirectionId),
                new SqlParameter("@Experience",obj.Experience)
            };

            return int.Parse(SQLHELP.GetDataTableWithParm(sql, parm).Rows[0]["returnId"].ToString());
        }

        /// <summary>
        /// 删除玩家的一个单位
        /// </summary>
        /// <param name="playerId">playerId</param>
        /// <param name="playerObjectId">playerObjectId</param>
        /// <returns>bool</returns>
        public bool DeletePlayerObject(int playerId, int playerObjectId)
        {
            string sql = "delete PlayerObject where Id=@Id and PlayerId=@PlayerId";
            SqlParameter[] parm = new SqlParameter[] {
                new SqlParameter("@Id",playerObjectId),
                new SqlParameter("@PlayerId",playerId)
            };

            int count = SQLHELP.GetNonQueryWithParm(sql, parm);

            if (count != 1)
            {
                return false;
            }
            return true;
        }
    }
}
